if(window.plus){
	plusReady();
}else{
	document.addEventListener('plusready',plusReady,false);
}
function plusReady () {
	
	//初始化选择日期
	$(".date_init").val(function() {		
		var date = new Date(); //日期对象
		var now = "";
		now = date.getFullYear()+"-";
		if(date.getMonth()+1 < 10){
			now = now + "0" + (date.getMonth()+1)+"-";
		}else{
			now = now + (date.getMonth()+1)+"-";
		}
		if(date.getDate() < 10){
			now = now + "0" + date.getDate();
		}else{
			now = now + date.getDate();
		}
		return now;
	});
	
	//初始化日期选择控件
	$(".form_datetime").datetimepicker({
		format: 'yyyy-mm-dd',
		language: 'zh-CN',
		weekStart:1,
		todayBtn:1,
		autoclose:1,
		todayHighlight:1,
		startView:2,
		minView:2,
		forceParse:0,
		startDate: '2014-01-01',
		endDate:'2020-12-31',
		keyboardNavigation:true
	});
	
	var userInfo = getData("user_data");
	
	$(document).ready(function() {
		UpdateState();
		if(userInfo == null || userInfo.type != "success"){
			alert('用户没有登录');
			window.location.href='login.html';
		}
		//初始化用户名
		$("#username").append(userInfo.msg.cname);
		
		queryMeal ();
		$("#date_submit").on("click", function () {
			queryMeal ();
		});
	});
	
	//查询处理
	function queryMeal () {
		var flightDate = $(".date_init").val();
		var flightNo = $("#flightNo").val();
		var item = flightDate + flightNo;
		if (flightNo == "") {
			alert("请输入航班号（AQ1021）！");
		}
		loadMealInfo (flightDate, flightNo);
	}
	
	//根据日期和航班号，向服务器请求数据，返回请求状态
	function loadMealInfo (flightDate, flightNo) {
		var item ="PsgInfoSimple"+flightDate+flightNo;
		console.log(isOnLine)
		if (!isOnLine) {
			showMealList (item);
		} else{
			var url = server_url+'/9airweb/service/queryqervice.action?flightno='+flightNo+'&flightdate='+flightDate;
			url += url_tail;
			console.log(url);
			
			$("#caseVerteClaire").show();
			$(".panel").hide();
			
			$.ajax({
				dataType: "json",
			    url: url,
			    timeout:12000
			}).done(function(backdata, status){
				if (status=='success') {
					if (setData(item, backdata)) {
						console.log("数据更新成功！");
						showMealList (item);
					}
				}
			}).fail(function(jqXHR, textStatus){
			    if(textStatus == 'timeout') {
			    	alert("数据更新失败，即将加载本地数据！");
			        showMealList (item);
			    }
			});
			
		}
			
	}
	
	//显示餐食具体信息
	function showMealList (item) {
		
		$("#caseVerteClaire").hide();
		$(".panel").show();
		emptyMealList();
		var mealList = getData(item);
		
		if (mealList.type == "success") {
			$.each(mealList.msg, function(key, value) {
				var meal_list = '<tr>'
			    					+'<td>'+ (key+1) +'</td>'
			    					+'<td>'+ value.dt +'</td>'
			    					+'<td>'+ value.fno +'</td>'
			    					+'<td>'+ value.name +'</td>'
			    					+'<td>'+ value.amount +'</td>'
			    					+'<td>'+ value.o_psg_name +'</td>'
			    					+'<td>'+ value.seatno +'</td>'
			    					+'<td>'+ value.seg +'</td>'
			    				+'</tr>';
				 $("#mealList").append(meal_list);
			});
		}
		
	}
	function emptyMealList () {
		$("#mealTotal").empty();
		$("#mealList").empty();
	}
}